<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
  </head>
  <body>
    <script>
      function Vue({ data }) {
        this._data = data;

        for (let key in this._data) {
          Object.defineProperty(this, key, {
            get: function ProxyGetter() {
              return this._data[key];
            },
            set: function ProxySetter(val) {
              this._data[key] = val;
            },
          });
        }

        // for (let key in this._data) {
        //   let value = this._data[key];

        //   Object.defineProperty(this._data, key, {
        //     get: function reactiveGetter() {
        //       console.log("获取数据劫持成功");
        //       return value;
        //     },
        //     set: function reactiveSetter(val) {
        //       console.log("修改数据拦截成功");
        //       value = val;
        //     },
        //   });
        // }

        for (let key in this._data) {
          let value = this._data[key];

          Object.defineProperty(this._data, key, {
            get: function reactiveGetter() {
              console.log("获取数据劫持成功");
              return value;
            },
            set: function reactiveSetter(val) {
              console.log("修改数据劫持成功");
              value = val;
            },
          });
        }
      }

      let vm = new Vue({
        data: {
          name: "pp",
          age: 23,
          gender: "male",
        },
      });

      console.log(vm);
    </script>
  </body>
</html>
